<?php

/**
 * Payment form.
 *
 * @package    form
 * @subpackage Payment
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class PaymentPXForm extends BasePaymentForm
{
  public function configure()
  {
	
			unset(
	      $this['created_at'], $this['updated_at'], $this['client_id']
	    );
	    $this->widgetSchema['person_id'] = new sfWidgetFormInputHidden();

		$cardsForm = new sfForm();
		if (!$this->isNew()) {  
	   // embed all old Phone forms
	   foreach ($this->getObject()->getPaymentCard() as $card) {
	   	$cardsForm->embedForm('card'.$card->getId(), new PaymentCardForm($card));
	   }
		}
		//create blank phone form at end
		 $cardsForm->embedForm('card', new PaymentCardForm());
		 $this->embedForm('cards', $cardsForm);
	
	
	
	
		$banksForm = new sfForm();
		if (!$this->isNew()) {  
	   // embed all old Phone forms
	   foreach ($this->getObject()->getPaymentBank() as $bank) {
	   	$banksForm->embedForm('bank'.$bank->getId(), new PaymentBankForm($bank));
	   }
		}
		//create blank phone form at end
		 $banksForm->embedForm('bank', new PaymentBankForm());
		 $this->embedForm('banks', $banksForm);
	
	
	
	
	
		$onlineprovidersForm = new sfForm();
		if (!$this->isNew()) {  
	   // embed all old Phone forms
	   foreach ($this->getObject()->getPaymentOnlineProvider() as $onlineProvider) {
	   	$onlineprovidersForm->embedForm('onlineProvider'.$onlineProvider->getId(), new PaymentOnlineProviderForm($onlineProvider));
	   }
		}
		//create blank phone form at end
		 $onlineprovidersForm->embedForm('onlineProvider', new PaymentOnlineProviderForm());
		 $this->embedForm('onlineProviders', $onlineprovidersForm);
	
	
	
		$remarksForm = new sfForm();
		if (!$this->isNew()) {  
	   // embed all old Phone forms
	   foreach ($this->getObject()->getPaymentRemarks() as $remark) {
	   	$remarksForm->embedForm('remark'.$remark->getId(), new PaymentRemarksForm($remark));
	   }
		}
		//create blank phone form at end
		 $remarksForm->embedForm('remark', new PaymentRemarksForm());
		 $this->embedForm('remarks', $remarksForm);
	
	
  }

  /**
   * Update the client_id of all the forms after the booking has been saved
   */
  public function saveEmbeddedForms($con = null, $forms = null)
  {    
    foreach($this->embeddedForms['banks']->getEmbeddedForms() as $bankForm)
    {
      if (!$bankForm->getObject()->getPaymentId())
      {
        $bankForm->getObject()->setPaymentId($this->getObject()->getId());
				//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
      }
    }

		foreach($this->embeddedForms['cards']->getEmbeddedForms() as $cardForm)
    {
      if (!$cardForm->getObject()->getPaymentId())
      {
        $cardForm->getObject()->setPaymentId($this->getObject()->getId());
				//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
      }
    }

		foreach($this->embeddedForms['onlineProviders']->getEmbeddedForms() as $onlineproviderForm)
    {
      if (!$onlineproviderForm->getObject()->getPaymentId())
      {
        $onlineproviderForm->getObject()->setPaymentId($this->getObject()->getId());
				//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
      }
    }

		foreach($this->embeddedForms['remarks']->getEmbeddedForms() as $remarksForm)
    {
      if (!$remarksForm->getObject()->getPaymentId())
      {
        $remarksForm->getObject()->setPaymentId($this->getObject()->getId());
				//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
      }
    }


		
    parent::saveEmbeddedForms($con, $forms);
	}


	public function bind(array $taintedValues = null, array $taintedFiles = null)
	{
		if (is_null($taintedValues['cards']['card']['card_id'])|| strlen($taintedValues['cards']['card']['card_id']) === 0 )
	  {
	    unset($this->embeddedForms['cards']['card']);
	    $this->validatorSchema['cards']['card'] = new sfValidatorPass();
	    $taintedValues['cards']['card'] = null;
	  }
		
		if (is_null($taintedValues['onlineProviders']['onlineProvider']['onlineprovider_id'])|| strlen($taintedValues['onlineProviders']['onlineProvider']['onlineprovider_id']) === 0 )
	  {
	    unset($this->embeddedForms['onlineProviders']['onlineProvider']);
	    $this->validatorSchema['onlineProviders']['onlineProvider'] = new sfValidatorPass();
	    $taintedValues['onlineProviders']['onlineProvider'] = null;
	  }
	
	
	  if (is_null($taintedValues['banks']['bank']['bank_id']) || strlen($taintedValues['banks']['bank']['bank_id']) === 0 ) 
	  {
	    unset($this->embeddedForms['banks']['bank']);
	    $this->validatorSchema['banks']['bank'] = new sfValidatorPass();
	    $taintedValues['banks']['bank'] = null;
	  }
	
	  if (is_null($taintedValues['remarks']['remark']['remarks']) || strlen($taintedValues['remarks']['remark']['remarks']) === 0 ) 
	  {
	    unset($this->embeddedForms['remarks']['remark']);
	    $this->validatorSchema['remarks']['remark'] = new sfValidatorPass();
	    $taintedValues['remarks']['remark'] = null;
	  }

	

	
		 parent::bind($taintedValues, $taintedFiles);
	}


}